﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using UIShare;
using EFong.View.Proxy;
using EFong.View.Customer;
using EFong.View.Base;
using DevExpress.XtraLayout.Utils;

namespace EFong.View.FlightTicket
{
    public partial class Frm_FlightTicket : BasicMdiChildForm
    {
        private Proxy.BusinessServiceProxy _businessProxy { get { return Proxy.BusinessServiceProxy.GetInstance(); } }
        private Proxy.PrintServiceProxy _printServiceProxy { get { return Proxy.PrintServiceProxy.GetInstance(); } }
        private Proxy.MasterServiceProxy _masterProxy { get { return Proxy.MasterServiceProxy.GetInstance(); } }

        public Frm_FlightTicket()
        {
            InitializeComponent();

            txt_Passenger.ReadOnly(true);
            txt_TicketOrderRef.ReadOnly(true);
            txt_TicketNo.ReadOnly(true);
            ckb_Keep.ReadOnly(true);
            txt_JourneyOrderType.ReadOnly(true);
            cmb_JourneyOrder.ReadOnly(true);
            //txt_InsuranceVendor.ReadOnly(true);
            txt_InsurancePoliceType.ReadOnly(true);
            cmb_InsurancePolice.ReadOnly(true);
            spe_Price.ReadOnly(true);
            spe_Tax1.ReadOnly(true);
            spe_Tax2.ReadOnly(true);
            spe_InsurancePolicePrice.ReadOnly(true);
            spe_InsuranceFee.ReadOnly(true);
            ckb_InsuranceFee.ReadOnly(true);
            spe_VendorDisount.ReadOnly(true);
            spe_Cost.ReadOnly(true);
            spe_NetPrice.ReadOnly(true);
            spe_Discount.ReadOnly(true);
            spe_Intergral.ReadOnly(true);
            txt_Customer.ReadOnly(true);
            txt_Vendor.ReadOnly(true);
            txt_VendorCurrency.ReadOnly(true);
            txt_OrderTicketUser.ReadOnly(true);
            txt_ProvideTicketUser.ReadOnly(true);
            dte_ProvideTicketDate.ReadOnly(true);
            dte_DeliveryTicketDate.ReadOnly(true);
            txt_CreditTerm.ReadOnly(true);
            spe_ExchangeRate.ReadOnly(true);
            spe_PrintTotalPrice.ReadOnly(true);
            rad_Internal.ReadOnly(true);
            rad_International.ReadOnly(true);
            txt_OrderRemark.ReadOnly(true);
            txt_Address.ReadOnly(true);
            txt_BankAccount.ReadOnly(true);
            txt_PaymentType.ReadOnly(true);

            txt_CPerson.ReadOnly(true);
            txt_CPersonTel.ReadOnly(true);
            txt_CPersonPhone.ReadOnly(true);

            spe_VendorDisount.SetValue(null);
            spe_Cost.SetValue(null);
            spe_NetPrice.SetValue(null);
            spe_Intergral.SetValue(null);
            spe_Discount.SetValue(null);

            cmb_JourneyType.ReadOnly(true);

            lct_BrowserReturn.Visibility = LayoutVisibility.Never;

            this.bbt_Void.Visibility = AppParameters.IsAdRole ? DevExpress.XtraBars.BarItemVisibility.Always : DevExpress.XtraBars.BarItemVisibility.Never;
        }

        protected override void InitFields()
        {
        }

        protected override void Events()
        {
            this.gv_Orders.FocusedRowChanged += (s, e) =>
            {
                Contract.RFlightOrder order = this.gv_Orders.GetRow(e.FocusedRowHandle) as Contract.RFlightOrder;

                if (order != null)
                {
                    Order = _businessProxy.GetFlightOrderByID(order.ID).ReturnValue;
                }
                else
                {
                    Order = null;
                }
            };

            this.gv_Passengers.FocusedRowChanged += (s, e) =>
            {
                Detail = this.gv_Passengers.GetRow(e.FocusedRowHandle) as Contract.FlightOrderDatail; ;
            };

            this.btn_CustomerData.Click += (s, e) =>
            {
                browserCustomerData();
            };
            this.btn_Search.Click += (s, e) =>
            {
                searchOrders();
            };
            this.btn_Clear.Click += (s, e) =>
            {
                clearSearchConditions();
            };

            this.btn_BrowserReturn.Click += (s, e) =>
            {
                if (Detail == null)
                    return;
                if (Detail.Status != "已退票")
                    return;

                Frm_FlightTicketReturn frm = new Frm_FlightTicketReturn();
                frm.ViewState = ViewStates.BROWSER;
                frm.Load += (s1, e1) =>
                {
                    Contract.FlightReturn flightReturn = Detail.FlightReturnHis[0];
                    flightReturn.FlightOrderDetail = Detail;
                    flightReturn.Currency = Order.Currency;
                    flightReturn.Customer = Order.Customer;
                    flightReturn.VendorCurrency = Order.VendorCurrency;
                    flightReturn.Currency = Order.Currency;
                    frm.Detail = flightReturn;
                };
                FormOpener.ShowDialog(frm);
            };
        }

        protected override void LoadDatas()
        {
            bds_Customer.BindingData(_masterProxy.GetAllCustomer().ReturnValue);
            bds_Vendor.BindingData(_masterProxy.GetAllFlightVendor().ReturnValue);

            //bds_FlightOrder.BindingData(new List<Contract.RFlightOrder>());

            clearSearchConditions();

            SetAuthorities();
        }

        protected void SetAuthorities()
        {
            foreach (Contract.RoleAuthorizedItem roleAuthorizedItem in AppParameters.LoginUser.Role.Authorities)
            {
                switch (roleAuthorizedItem.AuthorizedItem.Name)
                {
                    case "飞机票预定":
                        bbt_Add.Enabled = roleAuthorizedItem.Allow;
                        bbt_Edit.Enabled = roleAuthorizedItem.Allow;
                        break;
                    case "高级修改":
                        bbt_Void.Enabled = roleAuthorizedItem.Allow;
                        break;
                }
            }
        }

        private void bar_Main_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "bbt_Add":
                    this.add();
                    break;
                case "bbt_Edit":
                    this.edit();
                    break;
                case "bbt_Void":
                    this.voidOrder();
                    break;
                case "bbt_Print":
                    this.printOrder();
                    break;
                case "bbt_Close":
                    this.Close();
                    break;
                case "bbt_打印改票单":
                    this.打印改票单();
                    break;
                default:
                    break;
            }
        }

        private void printOrder()
        {
            if (Order == null)
                return;

            BasicPrinterForm frm = new BasicPrinterForm();
            frm.Text = "打印订单";

            Print.Rpt_FlightTicket report = new Print.Rpt_FlightTicket();
            //report.Order = Order;
            report.Order = _printServiceProxy.GetPrintFlightOrder(Order.ID).ReturnValue;
            //_printServiceProxy.GetPrintFlightOrder(Order.ID);

            frm.Report = report;

            FormOpener.ShowDialog(frm);
        }

        private void 打印改票单()
        {
            if (Order == null)
                return;

            Contract.FlightOrderDatail flightOrderDatail = this.gv_Passengers.GetFocusedRow() as Contract.FlightOrderDatail;
            Contract.RFlightChange flightChange = this.gv_ChangeHis.GetFocusedRow() as Contract.RFlightChange;

            if(flightOrderDatail == null || flightChange == null)
            {
                MessageBox.Show("没有选择改票单");
                return;
            }
            if (!flightOrderDatail.FlightChangeHis.Exists(fc => fc.ID == flightChange.ID))
            {
                MessageBox.Show("请选择正确的机票");
                return;
            }

            BasicPrinterForm frm = new BasicPrinterForm();
            frm.Text = "打印改票单";

            Print.Rpt_FilghtChange report = new Print.Rpt_FilghtChange();
            //report.Order = Order;
            var order1 = _printServiceProxy.GetPrintFlightOrder(Order.ID).ReturnValue;
            report.Order = order1;
            report.FlightChange = flightChange;
            //_printServiceProxy.GetPrintFlightOrder(Order.ID);
            report.FlightOrderSource.DataSource = order1;
            report.FlightOrderDetailSource.DataSource = flightOrderDatail;
            report.FlightChangeSource.DataSource = flightChange;

            frm.Report = report;

            FormOpener.ShowDialog(frm);
        }

        private void voidOrder()
        {
            bool isSure = false;
            MsgBox.Question("确定将订单作废?", () => { isSure = true; }, null);
            if (!isSure)
                return;

            if (Order == null)
                return;

            Frm_Void frm = new Frm_Void(Order);
            frm.Text = "订单作废";
            if (FormOpener.ShowDialog(frm) == DialogResult.OK)
                searchOrders();
        }

        private void add()
        {
            Frm_FlightTicketEdit frm = new Frm_FlightTicketEdit();
            frm.Text = "增加订单";
            frm.ViewState = ViewStates.ADD;
            frm.Load += (s, e) =>
            {
                frm.Order = new Contract.FlightOrder();
            };
            FormOpener.ShowDialog(frm);

            if (frm.ReturnValue != null && this.bds_FlightOrder.DataSource is List<Contract.RFlightOrder>)
            {
                this.bds_FlightOrder.Insert(0, frm.ReturnValue);
            }
        }

        private void edit()
        {
            if (Order == null)
                return;
            if (Order.Status == MsgResource.VoidStatus)
                return;

            Frm_FlightTicketEdit frm = new Frm_FlightTicketEdit();
            frm.Text = "修改订单资料";
            frm.ViewState = ViewStates.EDIT;
            frm.ChangedOrReturnedAction = () => { updateChange(); };
            frm.Load += (s, e) =>
            {
                frm.Order = Order;
            };
            if (FormOpener.ShowDialog(frm) == DialogResult.OK)
            {
                this.gv_Orders.BeginUpdate();
                this.gv_Orders.EndUpdate();
                Order = frm.Order;
            }
        }

        private void browserCustomerData()
        {
            if (Order == null)
                return;

            Frm_CustomerEdit frm = new Frm_CustomerEdit();
            frm.Text = "客户详细资料";
            frm.ViewState = ViewStates.BROWSER;
            frm.Load += (s, e) =>
            {
                frm.CurCust = Order.Customer;
            };
            FormOpener.ShowDialog(frm);
        }

        private void searchOrders()
        {
            List<Contract.RFlightOrder> flightOrderSource = new List<Contract.RFlightOrder>();

            if (rad_SCustomer.Checked)
            {
                flightOrderSource = _businessProxy.GetFlightOrderByCustomer(this.cmb_SearchCustomer.GetValue() as Contract.RCustomer).ReturnValue;
            }
            else if (rad_SOrderDate.Checked)
            {
                flightOrderSource = _businessProxy.GetFlightOrderByOrderDate(this.dte_SearchStartDate.GetValue(AppParameters.FetureDateTime), this.dte_SearchEndDate.GetValue(AppParameters.FetureDateTime).AddDays(1)).ReturnValue;
            }
            else if (rad_SOrderRef.Checked)
            {
                flightOrderSource = _businessProxy.GetFlightOrderByRef(this.txt_SearchOrderRef.GetText()).ReturnValue;
            }
            else if (rad_SVendor.Checked)
            {
                flightOrderSource = _businessProxy.GetFlightOrderByVendor(this.cmb_SearchVendor.GetValue() as Contract.RVendor).ReturnValue;
            }
            else if (rad_SPassenger.Checked)
            {
                flightOrderSource = _businessProxy.GetFlightOrderByPassenger(this.txt_SearchPassenger.GetText()).ReturnValue;
            }
            else if (rad_STicketNo.Checked)
            {
                flightOrderSource = _businessProxy.GetFlightOrderByTicketNo(this.txt_SearchTicketNo.GetText()).ReturnValue;
            }
            else if (rad_SFlightOrderRef.Checked)
            {
                flightOrderSource = _businessProxy.GetFlightOrderByAirwaysOrderNo(this.txt_SearchFlightOrderRef.GetText()).ReturnValue;
            }
            else if (rad_SJourneyNo.Checked)
            {
                flightOrderSource = _businessProxy.GetFlightOrderByJourneyOrder(this.txt_SearchJourneyNo.GetText()).ReturnValue;
            }
            else if (chk留用.Checked)
            {
                flightOrderSource = _businessProxy.GetFlightOrderByIsKeep(this.chk是否留用.GetValue()).ReturnValue;
            }
            else
            {
                flightOrderSource = _businessProxy.GetAllFlightOrder().ReturnValue;
            }

            bool sourceIsNull = flightOrderSource == null;
            bool sourceIsEmpty = !sourceIsNull && flightOrderSource.Count == 0;

            if (sourceIsEmpty)
            {
                MsgBox.Info("没有找到符合条件的订单.");
                this.Order = null;
                this.bds_FlightOrder.BindingData(new List<Contract.OtherBusiness>());
            }
            else
            {
                foreach (var order in flightOrderSource)
                {
                    if (order.Status == "已作废") 
                        order.CustomerStatus = "已作废";
                }

                this.bds_FlightOrder.BindingData(flightOrderSource);
            }
        }

        private void clearSearchConditions()
        {
            this.rad_SCustomer.SetValue(false);
            this.rad_SOrderDate.SetValue(false);
            this.rad_SOrderRef.SetValue(false);
            this.rad_SVendor.SetValue(false);
            this.rad_SFlightOrderRef.SetValue(false);
            this.rad_SJourneyNo.SetValue(false);
            this.rad_SPassenger.SetValue(false);
            this.rad_STicketNo.SetValue(false);
            this.chk留用.SetValue(false);
            this.chk是否留用.SetValue(false);
            ControlExtensions.ClearValue(
                this.cmb_SearchCustomer,
                this.cmb_SearchVendor,
                this.dte_SearchEndDate,
                this.dte_SearchStartDate,
                this.txt_SearchOrderRef,
                this.txt_SearchFlightOrderRef,
                this.txt_SearchJourneyNo,
                this.txt_SearchPassenger,
                this.txt_SearchTicketNo
                );

            searchOrders();
        }

        #region Order
        // current FlightOrder
        private Contract.FlightOrder _order;
        public Contract.FlightOrder Order
        {
            get { return _order; }
            set
            {
                _order = value;
                onOrderChanged();
            }
        }

        private void onOrderChanged()
        {
            if (Order == null)
            {
                ControlExtensions.ClearValue(
                    cmb_OrderUser,
                    dte_OrderDate,
                    txt_Customer,
                    //txt_Vendor,
                    txt_CreditTerm,
                    //spe_ExchangeRate,
                    spe_PrintTotalPrice,
                    txt_OrderRemark,
                    dte_ProvideTicketDate,
                    txt_Address,
                    txt_OrderTotalIntegral,
                    txt_OrderTotalPrice,
                    txt_OrderTotalProfit,
                    txt_OrderTotalVendorPrice,
                    txt_OrderTicketUser,
                    txt_ProvideTicketUser,
                    //txt_PaymentType,
                    //txt_BankAccount,
                    //txt_VendorCurrency,
                    txt_CPerson,
                    txt_CPersonTel,
                    txt_CPersonPhone,
                    dte_DeliveryTicketDate
                    );

                rad_Internal.SetValue(false);
                rad_International.SetValue(false);
                this.bds_flightOrderDatail.BindingData(null);
            }
            else
            {
                foreach (var detail in Order.Details)
                {
                    detail.FlightOrder = Order;
                    foreach (var fr in detail.FlightReturnHis)
                        fr.FlightOrderDetail = detail;
                }

                if (Order.OrderUser != null)
                    this.cmb_OrderUser.SetValue(Order.OrderUser);
                if (Order.OrderDate != null)
                    this.dte_OrderDate.SetValue(Order.OrderDate);
                this.txt_OrderRef.SetText(Order.Ref);
                this.txt_Customer.SetText(Order.Customer);
                //this.txt_Vendor.SetText(Order.Vendor);
                this.txt_CreditTerm.SetText(Order.CreditTerm);
                //this.spe_ExchangeRate.SetValue(Order.ExchangeRate);
                this.spe_PrintTotalPrice.SetValue(Order.PrintTotalPrice);
                this.txt_OrderRemark.SetText(Order.Remark);

                this.dte_ProvideTicketDate.SetValue(Order.ProvideTicketDate);
                this.dte_DeliveryTicketDate.SetValue(Order.DeliveryDate);

                rad_International.SetValue(Order.IsInternational);
                if (Order.IsInternational)
                    rad_International.SetValue(true);
                else
                    rad_Internal.SetValue(true);

                txt_OrderTicketUser.SetText(Order.OrderTicketUser);
                txt_ProvideTicketUser.SetText(Order.ProvideTicketUser);

                //txt_PaymentType.SetText(Order.PaymentType);
                //txt_BankAccount.SetText(Order.BankAccount);
                //if (Order.VendorCurrency != null)
                //    this.txt_VendorCurrency.SetText(Order.VendorCurrency);

                txt_CPerson.SetText(Order.ContactPerson);
                txt_CPersonTel.SetText(Order.联系电话);
                txt_CPersonPhone.SetText(Order.移动电话);

                this.txt_Address.SetText(Order.Address);

                this.txt_OrderTotalPrice.SetText(Order.TotalAccountReceivable);
                this.txt_OrderTotalProfit.SetText(Order.TotalProfit);
                this.txt_OrderTotalVendorPrice.SetText(Order.TotalAccountPayable);
                this.txt_OrderTotalIntegral.SetText(Order.TotalIntergral);

                this.bds_flightOrderDatail.BindingData(Order.Details);

                this.bds_flightReturn.BindingData(Order.FlightReturnHis);
                //updateReturn();

                this.bds_flightChange.BindingData(Order.FlightChangeHis);
                updateChange();
            }
        }

        //private void updateReturn()
        //{
        //lcg_Return.Visibility = this.bds_flightReturn.Count > 0 ? LayoutVisibility.Always : LayoutVisibility.Never;
        //}

        private void updateChange()
        {
            lcg_Change.Visibility = this.bds_flightChange.Count > 0 ? LayoutVisibility.Always : LayoutVisibility.Never;
        }
        #endregion

        #region Detail
        private Contract.FlightOrderDatail _detail;
        public Contract.FlightOrderDatail Detail
        {
            get { return _detail; }
            set
            {
                _detail = value;
                onDetailChanged();
            }
        }

        private void onDetailChanged()
        {
            if (Detail == null)
            {
                this.bds_flightWay.BindingData(typeof(Contract.FlightWay));
                setJourneyType();

                Passenger = null;
                //JourneyOrder = null;
                //InsurancePolicy = null;
                this.cmb_InsurancePolice.Properties.BindingData(null);
                this.cmb_JourneyOrder.Properties.BindingData(null);
                ControlExtensions.ClearValue(
                    txt_TicketOrderRef,
                    txt_TicketNo,
                    spe_Price,
                    spe_Tax1,
                    spe_Tax2,
                    spe_InsuranceFee,
                    spe_VendorDisount,
                    spe_Cost,
                    spe_NetPrice,
                    spe_Discount,
                    txt_TotalPrice,
                    txt_TotalProfit,
                    txt_TotalVendorPrice,
                    spe_Intergral,
                    txt_Vendor,
                    txt_PaymentType,
                    txt_BankAccount,
                    txt_VendorCurrency,
                    spe_ExchangeRate,
                    cmb_JourneyType,
                    cmb_JourneyOrder,
                    cmb_InsurancePolice
                    );

                ckb_InsuranceFee.SetValue(false);
                ckb_Keep.SetValue(false);
            }
            else
            {
                this.bds_flightWay.BindingData(Detail.FlightWayList.FilterIsDelFlightWays());
                setJourneyType();

                Passenger = Detail.Passenger;

                txt_TicketOrderRef.SetText(Detail.AirwaysOrderNo);
                txt_TicketNo.SetText(Detail.TicketNo);

                if (Detail.Status == "留用")
                    ckb_Keep.SetValue(true);
                else
                    ckb_Keep.SetValue(false);

                //JourneyOrder = Detail.JourneyOrder;
                if (Detail.JourneyOrderList.Count > 0)
                {
                    txt_JourneyOrderType.SetText(Detail.JourneyOrderList[0].Type);
                }
                else
                {
                    txt_JourneyOrderType.SetText(string.Empty);
                }
                cmb_JourneyOrder.Properties.BindingData<Contract.JourneyOrder>(Detail.JourneyOrderList, (item) => { return true; });

                //InsurancePolicy = Detail.InsurancePolicy;
                if (Detail.InsurancePolicyList.Count > 0)
                {
                    txt_InsurancePoliceType.SetText(Detail.InsurancePolicyList[0].Type);

                }
                else
                {
                    txt_InsurancePoliceType.SetText(string.Empty);
                }
                cmb_InsurancePolice.Properties.BindingData<Contract.InsurancePolicy>(Detail.InsurancePolicyList, (item) => { return true; });

                spe_Price.SetValue(Detail.Price);
                spe_Tax1.SetValue(Detail.Tax1);
                spe_Tax2.SetValue(Detail.Tax2);
                spe_InsuranceFee.SetValue(Detail.InsuranceFee);
                ckb_InsuranceFee.SetValue(Detail.IsInsurancePolicyFree);
                spe_VendorDisount.SetValue(Detail.VendorDiscount);
                spe_Cost.SetValue(Detail.Cost);
                spe_NetPrice.SetValue(Detail.NetPrice);
                spe_Discount.SetValue(Detail.Discount);
                spe_Intergral.SetValue(Detail.Intergral);

                txt_Vendor.SetText(Detail.Vendor);
                txt_PaymentType.SetText(Detail.PaymentType);
                txt_BankAccount.SetText(Detail.BankAccount);
                if (Detail.VendorCurrency != null)
                    this.txt_VendorCurrency.SetText(Detail.VendorCurrency);
                spe_ExchangeRate.SetValue(Detail.ExchangeRate);

                this.txt_TotalPrice.SetText(Detail.AccountReceivable);
                this.txt_TotalProfit.SetText(Detail.Profit);
                this.txt_TotalVendorPrice.SetText(Detail.AccountPayable);

                //this.bds_flightChange.BindingData(Detail.FlightChangeHis);
                //this.bds_flightReturn.BindingData(Detail.FlightReturnHis);

                this.lct_BrowserReturn.Visibility = Detail.Status == "已退票" ? LayoutVisibility.Always : LayoutVisibility.Never;
            }
        }

        private void setJourneyType()
        {
            if (bds_flightWay.Count == 0)
            {
                this.cmb_JourneyType.SelectedIndex = -1;
            }
            else if (bds_flightWay.Count == 1)
            {
                this.cmb_JourneyType.SelectedIndex = 0;
            }
            else if (bds_flightWay.Count == 2 && ((Contract.FlightWay)bds_flightWay[0]).DepartLocation == ((Contract.FlightWay)bds_flightWay[1]).Destination &&
                    ((Contract.FlightWay)bds_flightWay[0]).Destination == ((Contract.FlightWay)bds_flightWay[1]).DepartLocation)
            {
                this.cmb_JourneyType.SelectedIndex = 1;
            }
            else
            {
                this.cmb_JourneyType.SelectedIndex = 2;
            }
        }
        #endregion

        #region Passenger
        private Contract.Passenger _passenger;
        public Contract.Passenger Passenger
        {
            get { return _passenger; }
            set
            {
                _passenger = value;
                onPassengerChanged();
            }
        }

        private void onPassengerChanged()
        {
            if (Passenger == null)
            {
                ControlExtensions.ClearValue(
                    txt_Passenger,
                    txt_Sex,
                    txt_Phone,
                    txt_IDCard);
            }
            else
            {
                txt_Passenger.SetText(Passenger);
                txt_Sex.SetText(Passenger.Sex);

                string phote = string.Empty;
                if (!string.IsNullOrEmpty(Passenger.Tel1) && !string.IsNullOrEmpty(Passenger.Tel2))
                    phote = string.Format("{0} ; {1}", Passenger.Tel1, Passenger.Tel2);
                else
                    phote = string.IsNullOrEmpty(Passenger.Tel1) ? Passenger.Tel2 : Passenger.Tel1;

                txt_Phone.SetText(phote);

                string idCard = string.Empty;
                if (!string.IsNullOrEmpty(Passenger.IDCardNr1) && !string.IsNullOrEmpty(Passenger.IDCardNr2))
                    idCard = string.Format("{0} ; {1}", Passenger.IDCardNr1, Passenger.IDCardNr2);
                else
                    idCard = string.IsNullOrEmpty(Passenger.IDCardNr1) ? Passenger.IDCardNr2 : Passenger.IDCardNr1;

                txt_IDCard.SetText(idCard);
            }
        }
        #endregion

        #region JourneyOrder
        //private Contract.JourneyOrder _journeyOrder;
        //public Contract.JourneyOrder JourneyOrder
        //{
        //    get { return _journeyOrder; }
        //    set
        //    {
        //        _journeyOrder = value;
        //        onJourneyOrderChanged();
        //    }
        //}

        //private void onJourneyOrderChanged()
        //{
        //    if (JourneyOrder == null)
        //    {
        //        ControlExtensions.ClearValue(
        //            txt_JourneyOrderType,
        //            cmb_JourneyOrder
        //            );
        //    }
        //    else
        //    {
        //        txt_JourneyOrderType.SetText(JourneyOrder.Type);
        //        cmb_JourneyOrder.SetText(JourneyOrder.Ref);
        //    }
        //}
        #endregion

        #region InsurancePolicy
        //private Contract.InsurancePolicy _insurancePolicy;
        //public Contract.InsurancePolicy InsurancePolicy
        //{
        //    get { return _insurancePolicy; }
        //    set
        //    {
        //        _insurancePolicy = value;
        //        onInsurancePolicyChanged();
        //    }
        //}

        //private void onInsurancePolicyChanged()
        //{
        //    if (InsurancePolicy == null)
        //    {
        //        ControlExtensions.ClearValue(
        //            txt_InsuranceVendor,
        //            txt_InsurancePoliceType,
        //            cmb_InsurancePolice,
        //            spe_InsurancePolicePrice
        //            );
        //    }
        //    else
        //    {
        //        //txt_InsuranceVendor.SetText(Detail.InsurancePolicy.Vendor);
        //        //txt_InsurancePoliceType.SetText(Detail.InsurancePolicy.Type);
        //        //txt_InsurancePolice.SetText(Detail.InsurancePolicy.Ref);
        //        //spe_InsurancePolicePrice.SetValue(Detail.InsurancePolicy.BasePrice);
        //    }
        //}
        #endregion

        #region Vendor
        public Contract.Vendor SelVend { get; set; }
        #endregion

        #region Customer
        private Contract.Customer _selCust;
        public Contract.Customer SelCust
        {
            get { return _selCust; }
            set
            {
                _selCust = value;
                onSelCustChanged();
            }
        }

        private void onSelCustChanged()
        {
            if (SelCust == null)
            {
                this.txt_SalesMan.SetText(null);
                this.txt_Address.SetText(null);
            }
            else
            {
                this.txt_SalesMan.SetText(SelCust.Salesman);
                this.txt_Address.SetText(SelCust.Address);
            }
        }
        #endregion

    }
}
